/**
 * Copyright (c) 2008 Luciano Bustos - riaevolution.com, All Rights Reserved.
 *
 * Contact luchyx@riaevolution.com
 * See www.riaevolution.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is furnished to do
 * so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * */
package com.riaevolution.components.ToolTipGuide
{
	import flash.display.Graphics;
	import flash.geom.Point;

	import mx.core.UIComponent;
	import mx.skins.halo.HaloBorder;
	import mx.utils.NameUtil;

	/**
	 * ToolTipGuideSkinn class. This create and handle the arrow positioning
	 * <b>author</b> Luciano Bustos
	 */
	public class ToolTipGuideWindowBackground extends HaloBorder
	{
		public function ToolTipGuideWindowBackground()
		{
			super();
		}

		override protected function updateDisplayList(w:Number, h:Number):void
		{
			h=h - 4;
			super.updateDisplayList(w, h);
			if (this.parent && this.parent is ToolTipGuideWindow)
			{
				var guide:ToolTipGuideWindow=this.parent as ToolTipGuideWindow;
				var aw:Number=20;
				var ah:Number=10;

				var a:Point;
				var b:Point;
				var c:Point;
				//TopOut - TopInt
				if (guide.arrowPosition == ToolTipGuidePosition.TOP_OUT)
				{
					a=new Point(0, h);
					b=new Point(a.x + aw / 2, a.y + ah);
					c=new Point(a.x + aw, a.y);
				}
				//Apunto desde (Bottom Out)
				else if (guide.arrowPosition == ToolTipGuidePosition.BOTTOM_OUT)
				{
					a=new Point(0, 0);
					b=new Point(a.x + aw / 2, a.y - ah);
					c=new Point(a.x + aw, a.y);
				}
				//Apunto desde (Right Out) <
				else if (guide.arrowPosition == ToolTipGuidePosition.RIGHT_OUT)
				{
					//x,y
					a=new Point(0, h / 2);
					b=new Point(a.x, a.y - ah);
					c=new Point((a.x - aw) / 2, a.y - ah / 2);
				}
				//Apunto desde (Left Out) <
				else if (guide.arrowPosition == ToolTipGuidePosition.LEFT_OUT)
				{
					//x,y
					a=new Point(w, h / 2);
					b=new Point(a.x + ah, a.y + ah / 2);
					c=new Point(a.x, a.y + ah);
				}
				createArrow(this.graphics, a, b, c, guide.getStyle('backgroundColor'));
			}
		}

		private static function createArrow(graphics:Graphics, a:Point, b:Point, c:Point, color:int=0x000000, lineColor:int=0xCECECE):void
		{
			//graphics.clear();
			//esto recorre un triangulo de la forma ABC
			//      b
			//    /  \
			//   a -- c
			graphics.beginFill(0x000000);
			graphics.beginFill(color);
			graphics.moveTo(a.x, a.y);
			graphics.lineTo(b.x, b.y);
			graphics.lineStyle(1, lineColor);
			graphics.lineTo(c.x, c.y);
			graphics.lineTo(a.x, a.y);
			graphics.endFill();
		}
	}
}